<?php
/******************************************
 * add by allen.qiang 20170923
 * 共通功能控制器
 *****************************************/
namespace wap\controllers;
use Yii;
use yii\web\Controller;

class CommonController extends Controller{


    public function init()
    {
        $this->layout = false;
    }

//    public function beforeAction($action)
//    {
//        return true;
//    }

    //保存缓存数据
    public function setCache($key, $value, $timeout=0, $global = false)
    {
        if(!isset($_SESSION)) session_start();
        if ($global)
            $tmp_key = $key;
        else
            $tmp_key = session_id() . $key;
        Yii::$app->cache->set($tmp_key, $value, $timeout);
    }

    //获取缓存数据
    public function getCache($key, $global = false)
    {
        if(!isset($_SESSION)) session_start();
        if ($global)
            $tmp_key = $key;
        else
            $tmp_key = session_id() . $key;

        return Yii::$app->cache->get($tmp_key);
    }

    //删除缓存
    public function delCache($key, $global = false)
    {
        if(!isset($_SESSION)) session_start();
        if ($global)
            $tmp_key = $key;
        else
            $tmp_key = session_id() . $key;

        return Yii::$app->cache->delete($tmp_key);
    }

    public $userName = "";

    public function getLoginUser(){
       return $this->getCache('USER');
    }
    public function setLoginUser($userInfo){
        $this->setCache('USER', $userInfo, 0);
    }

}